#!/bin/bash
source `dirname $0`/../env.sh
Prog=python
FileName="$CRAWLER_PATH/i_manager/server.py"
Cut=${FileName%%.*}

start() {
    if [[ -f uwsgi.pid ]]; then
	echo 'i_manager server is running'
	exit 1
	fi
	uwsgi uwsgi.ini
	echo 'start'
}

stop() {
	uwsgi --stop uwsgi.pid
	pid=`cat uwsgi.pid`
	while [[ 1 ]]
	do
	pcnt=`ps -ux |grep  $pid|awk  '{print $2}'|wc -l`
	if [[ $pcnt != 1 ]];then
	 sleep 0.2
	 else
	 break
	 fi
	done
	rm uwsgi.pid
	echo 'stopped'
}

restart() {
	stop
	start
}


status() {
    if [[ -f uwsgi.pid ]]; then
    pid=`cat uwsgi.pid`
    pcnt=`ps -ux |grep $pid|awk  '{print $2}'|wc -l`
    if [[ $pcnt != 2 ]];then
    #不存在
    echo 0
    return 0
    else
    #存在
    echo 1
    return 1
    fi
    else
    #不存在
    echo 0
    return 0
    fi
}

case "$1" in
	start|stop|restart|status)
  		$1
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart}"
		exit 1
esac
